package com.astrob.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.astrob.model.FavoriteData;
import com.astrob.model.FavoriteFileHandle;
import com.astrob.nhcmap.R;
import com.astrob.util.Utils;

import java.util.ArrayList;

public class FavwenluAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<FavoriteData> mItemDatas = new ArrayList<FavoriteData>();
	private PoiItemListener listener = null;

	public FavwenluAdapter(Context context) {
		this.context = context;
		updataDatas(true);
	}

	public void setListener(PoiItemListener listener) {
		this.listener = listener;
	}

	public void updataDatas(boolean isFav) {
		mItemDatas.clear();
		ArrayList<FavoriteData> favlist = FavoriteFileHandle.getInstance()
				.getListFavorite();
		for (int i = 0; i < favlist.size(); i++) {
			FavoriteData data = favlist.get(i);
			mItemDatas.add(data);
		}
	}

	@Override
	public int getCount() {
		if (mItemDatas != null)
			return mItemDatas.size();
		else
			return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mItemDatas != null)
			return mItemDatas.get(position);
		else
			return null;
	}

	public FavoriteData getData(int position) {
		FavoriteData data = new FavoriteData();
		if (mItemDatas != null && position >= 0
				&& position < mItemDatas.size()) {
			data = mItemDatas.get(position);
		}
		return data;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private static int mSpotResId[] = { R.drawable.tourism_type01,
			R.drawable.tourism_type02, R.drawable.tourism_type03,
			R.drawable.tourism_type04, R.drawable.tourism_type05,
			R.drawable.tourism_type06, R.drawable.tourism_type07,
			R.drawable.tourism_type08,
			R.drawable.icon_note};

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		// 如果未选择或list列表为空时直接跳出，避免NullPointException
		ViewHolder holder;
		if (null == convertView) {
			convertView = View.inflate(context, R.layout.adapter_favwenlu_item,
					null);

			holder = new ViewHolder();

			holder.ivIcon = (ImageView) convertView
					.findViewById(R.id.item_icon);
			holder.tvName = (TextView) convertView.findViewById(R.id.item_name);
			holder.tvAddr = (TextView) convertView
					.findViewById(R.id.item_addpend1);
			holder.tvTime = (TextView) convertView
					.findViewById(R.id.item_addpend2);
			holder.ibDelete = (ImageButton) convertView
					.findViewById(R.id.itemdelete_btn);
			holder.ibInfo = (ImageButton) convertView
					.findViewById(R.id.item_btn);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (position < 0 || mItemDatas.size() <= 0
				|| position >= mItemDatas.size()) {
			return null;
		}
		int iconIndex = mItemDatas.get(position).getIcon();
		if (iconIndex >= 0 && iconIndex < mSpotResId.length) {
			holder.ivIcon.setBackgroundResource(mSpotResId[iconIndex]);
		}

		holder.tvName.setText(mItemDatas.get(position).getName());
		if (null == mItemDatas.get(position).getAddress()
				|| mItemDatas.get(position).getAddress().length() < 2) {
			holder.tvAddr.setVisibility(View.GONE);
		} else {
			holder.tvAddr.setVisibility(View.VISIBLE);
			holder.tvAddr.setText(mItemDatas.get(position).getAddress());
		}
		holder.tvTime.setText(mItemDatas.get(position).getDataTime());

		holder.ivIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (Utils.isFastDoubleClick())
					return;
				if (listener != null)
					listener.onShowFavPic(position);
			}
		});
		
		holder.ibInfo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (Utils.isFastDoubleClick())
					return;
				if (listener != null)
					listener.onEditinfo(position);
			}
		});

		holder.ibDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (Utils.isFastDoubleClick())
					return;
				if (listener != null)
					listener.onDelete(position);

			}
		});

		return convertView;
	}

	public void showFavInfo(int nposition) {
		Toast.makeText(context, mItemDatas.get(nposition).strExInfo,
				Toast.LENGTH_SHORT).show();
	}

	public interface PoiItemListener {
		void onEditinfo(int pos);

		void onDelete(int pos);
		
		void onShowFavPic(int pos);
	}

	static class ViewHolder {
		ImageView ivIcon;
		TextView tvName;
		TextView tvAddr;
		TextView tvTime;
		ImageButton ibDelete;
		ImageButton ibInfo;
	}
}
